package com.wj.facedetect.util;


import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.wj.facedetect.R;
import com.wj.facedetect.widget.AlertDialog;
import com.wj.facedetect.widget.MyEditText;
import com.wj.facedetect.widget.timeselector.TimeSelector;

import java.util.Date;

import static com.wj.facedetect.widget.timeselector.TimeSelector.FORMAT_STR_YMD;

public class DialogUtil {

    private static final String TAG = "DialogUtil";

    /**
     * 单按钮Dialog
     *
     * @param context
     * @param msg     内容显示
     */
    public static void showErrorMsg(Context context, String msg) {
        try {
            new AlertDialog(context).builder()
                    .setMsg(msg)
                    .setCancelable(false)
                    .setNegativeButton("确定", true, null).show();
        } catch (Exception e) {
            Log.e(TAG, "showErrorMsg", e);
        }
    }

    /**
     * 单按钮Dialog
     *
     * @param context
     * @param msg     内容显示
     */
    public static void showErrorMsgWithBack(Context context, String msg, View.OnClickListener onClickListener) {
        try {
            new AlertDialog(context).builder()
                    .setMsg(msg)
                    .setCancelable(false)
                    .setNegativeButton("确定", true, onClickListener).show();
        } catch (Exception e) {
            Log.e(TAG, "showErrorMsg", e);
        }
    }

    /**
     * @param context
     * @param title
     * @param msg
     * @param ok
     * @param cancel
     * @param onClickListener
     */
    public static AlertDialog showChooseDialog(Context context, String title, String msg, String ok, String cancel,
                                               View.OnClickListener onClickListener) {
        AlertDialog dialog = null;
        try {
            if (TextUtils.isEmpty(ok)) {
                ok = "确定";
            }
            if (TextUtils.isEmpty(cancel)) {
                cancel = "取消";
            }
            if (TextUtils.isEmpty(title)) {
                dialog = new AlertDialog(context).builder();
                dialog.setNegBtnTextColor(context.getResources().getColor(R.color.alertdialog_line));
                dialog.setPosBtnTextColor(context.getResources().getColor(R.color.head_color));
                dialog.setMsg(msg)
                        .setCancelable(false)
                        .setPositiveButton(ok, onClickListener).setNegativeButton(cancel, true, null).show();
            } else {
                dialog = new AlertDialog(context).builder().setTitle(title);
                dialog.setNegBtnTextColor(context.getResources().getColor(R.color.alertdialog_line));
                dialog.setPosBtnTextColor(context.getResources().getColor(R.color.head_color));
                dialog.setMsg(msg)
                        .setCancelable(false)
                        .setPositiveButton(ok, onClickListener).setNegativeButton(cancel, true, null).show();
            }
        } catch (Exception e) {
            Log.e(TAG, "showChooseDialog", e);
        }
        return dialog;
    }

    /**
     * 设置IP
     *
     * @param context
     * @param settingIpListener
     */
    public static void showInputIpDialog(Context context, final SettingIpListener settingIpListener) {
        try {
            WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            Display defaultDisplay = windowManager.getDefaultDisplay();
            View view = LayoutInflater.from(context).inflate(R.layout.inputip, null);
            LinearLayout lLayout_bg = view.findViewById(R.id.ll_bg);
            TextView tvSure = view.findViewById(R.id.tv_sure);
            TextView tvCancel = view.findViewById(R.id.tv_cancel);
            final MyEditText zwIp = view.findViewById(R.id.et_ip);
            final MyEditText gwIp = view.findViewById(R.id.et_gw_ip);
            final MyEditText id = view.findViewById(R.id.et_id);
            final Dialog dialog = new Dialog(context, R.style.AlertDialogStyle);
            String basUrl = PreferencesUtils.getInstance().getSharePreStrBaseUrl(PreferencesUtils.BASE_URL);
            String gwBaseUrl = PreferencesUtils.getInstance().getSharePreStrGwBaseUrl(PreferencesUtils.GW_BASE_URL);
            String clusterIdStr = PreferencesUtils.getInstance().getSharePreStrClusterId(PreferencesUtils.CLUSTER_ID);
            zwIp.setText(basUrl);
            gwIp.setText(gwBaseUrl);
            id.setText(clusterIdStr);
            dialog.setContentView(view);
            // 调整dialog背景大小
            lLayout_bg.setLayoutParams(new FrameLayout.LayoutParams((int) (defaultDisplay.getWidth() * 0.85), LinearLayout.LayoutParams.WRAP_CONTENT));
            tvSure.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String zw = zwIp.getText().toString().trim();
                    String gw = gwIp.getText().toString().trim();
                    String clusterId = id.getText().toString().trim();
                    if (TextUtils.isEmpty(zw)) {
                        ToastUtil.showMessage("专网IP不能为空");
                        return;
                    }
                    if (TextUtils.isEmpty(gw)) {
                        ToastUtil.showMessage("公网IP不能为空");
                        return;
                    }
                    if (TextUtils.isEmpty(clusterId)) {
                        ToastUtil.showMessage("集群id不能为空");
                        return;
                    }
                    settingIpListener.getIp(zw, gw, clusterId);
                    dialog.dismiss();
                }
            });
            tvCancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    dialog.dismiss();
                }
            });
            dialog.show();
        } catch (Exception e) {
            Log.e(TAG, "showChooseDialog", e);
        }

    }

    /**
     * 填写拒接原因
     *
     * @param context
     * @param settingIpListener
     */
    public static void showInputJjRwBz(Context context, final SettingIpListener settingIpListener) {
        try {
            WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            Display defaultDisplay = windowManager.getDefaultDisplay();
            View view = LayoutInflater.from(context).inflate(R.layout.input_jjrw, null);
            LinearLayout lLayout_bg = view.findViewById(R.id.ll_bg);
            TextView tvSure = view.findViewById(R.id.tv_sure);
            TextView tvCancel = view.findViewById(R.id.tv_cancel);
            final MyEditText zwIp = view.findViewById(R.id.edit_bz);
            final Dialog dialog = new Dialog(context, R.style.AlertDialogStyle);
            dialog.setContentView(view);
            // 调整dialog背景大小
            lLayout_bg.setLayoutParams(new FrameLayout.LayoutParams((int) (defaultDisplay.getWidth() * 0.85), LinearLayout.LayoutParams.WRAP_CONTENT));
            tvSure.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String zw = zwIp.getText().toString();
                    if (TextUtils.isEmpty(zw)) {
                        ToastUtil.showMessage("拒接原因不能为空");
                        return;
                    }
                    settingIpListener.getIp(zw, "", "");
                    dialog.dismiss();
                }
            });
            tvCancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    dialog.dismiss();
                }
            });
            dialog.show();
        } catch (Exception e) {
            Log.e(TAG, "showChooseDialog", e);
        }

    }

    /**
     * 显示照片
     *
     * @param context
     * @param bitmap
     */
    public static void showPhotoDialog(Context context, Bitmap bitmap) {
        try {
            WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            Display defaultDisplay = windowManager.getDefaultDisplay();
            View view = LayoutInflater.from(context).inflate(R.layout.activity_show_photo, null);
            TextView tvLeft = view.findViewById(R.id.tv_left);
            ImageView iv = view.findViewById(R.id.iv);
            final Dialog dialog = new Dialog(context, R.style.AlertDialogStyle);
            dialog.setContentView(view);
            dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            tvLeft.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    dialog.dismiss();
                }
            });
            iv.setImageBitmap(bitmap);
            dialog.show();
        } catch (Exception e) {
            Log.e(TAG, "showChooseDialog", e);
        }

    }

    /**
     * 展示照片
     *
     * @param context
     * @param bitmapUrl
     */
    public static void showPhotoDialog(Context context, String bitmapUrl) {
        try {
            WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            Display defaultDisplay = windowManager.getDefaultDisplay();
            View view = LayoutInflater.from(context).inflate(R.layout.activity_show_photo, null);
            TextView tvLeft = view.findViewById(R.id.tv_left);
            SimpleDraweeView iv = view.findViewById(R.id.iv);
            final Dialog dialog = new Dialog(context, R.style.AlertDialogStyle);
            dialog.setContentView(view);
            dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            tvLeft.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    dialog.dismiss();
                }
            });
            iv.setImageURI(bitmapUrl);
            dialog.show();
        } catch (Exception e) {
            Log.e(TAG, "showChooseDialog", e);
        }

    }

    /**
     * 选择照片或者拍照
     *
     * @param context
     */
    public static void showChoosePhotoOrTakePhotoDialog(Context context, View.OnClickListener onClickListenerPz, View.OnClickListener onClickListenerXzzp) {
        try {
            WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            Display defaultDisplay = windowManager.getDefaultDisplay();
            View view = LayoutInflater.from(context).inflate(R.layout.choose_or_take_photo, null);
            RelativeLayout rlPz = view.findViewById(R.id.rl_pz);
            RelativeLayout rlXzzp = view.findViewById(R.id.rl_xzzp);
            final Dialog dialog = new Dialog(context, R.style.AlertDialogStyle);
            dialog.setContentView(view);
            dialog.getWindow().setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            rlPz.setTag(dialog);
            rlXzzp.setTag(dialog);
            rlPz.setOnClickListener(onClickListenerPz);
            rlXzzp.setOnClickListener(onClickListenerXzzp);
            dialog.show();
        } catch (Exception e) {
            Log.e(TAG, "showChooseDialog", e);
        }

    }

    /***
     *选择订阅时间dialog
     */
    public static void showChooseDyTimeDialog(final Context context, final ChooseDyTimeListener chooseDyTimeListener) {
        try {
            WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            Display defaultDisplay = windowManager.getDefaultDisplay();
            View view = LayoutInflater.from(context).inflate(R.layout.add_plan_start_end_progress, null);
            final Dialog dialog = new Dialog(context, R.style.AlertDialogStyle);
            TextView tvCancel = view.findViewById(R.id.tv_cancel);
            TextView tvSure = view.findViewById(R.id.tv_sure);
            RelativeLayout rlKssj = view.findViewById(R.id.rl_qssj);
            RelativeLayout rlJzsj = view.findViewById(R.id.rl_jzsj);
            final TextView tvKsjs = view.findViewById(R.id.tv_date_ks);
            final TextView tvJzsj = view.findViewById(R.id.tv_date);
            rlKssj.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //选择起始时间
                    TimeSelector timeSelectorQssj = new TimeSelector(context, new TimeSelector.ResultHandler() {
                        @Override
                        public void handle(Date time) {
                            tvKsjs.setText(TimeUtils.getFormatTimeByType(time, FORMAT_STR_YMD));
                        }
                    }, "2000.01.01", "2100.12.30");
                    timeSelectorQssj.setMode(TimeSelector.MODE.YMD);
                    String qssj = tvKsjs.getText().toString().trim();
                    timeSelectorQssj.setNowTime(TextUtils.isEmpty(qssj) ? TimeUtils.getFormatTimeByType(System.currentTimeMillis(), FORMAT_STR_YMD) : TimeUtils.getFormatTimeByType(TimeUtils.getTimestamp(qssj, FORMAT_STR_YMD), FORMAT_STR_YMD));
                    timeSelectorQssj.show();
                }
            });
            rlJzsj.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //选择起始时间
                    TimeSelector timeSelectorQssj = new TimeSelector(context, new TimeSelector.ResultHandler() {
                        @Override
                        public void handle(Date time) {
                            tvJzsj.setText(TimeUtils.getFormatTimeByType(time, FORMAT_STR_YMD));
                        }
                    }, "2000.01.01", "2100.12.30");
                    timeSelectorQssj.setMode(TimeSelector.MODE.YMD);
                    String qssj = tvJzsj.getText().toString().trim();
                    timeSelectorQssj.setNowTime(TextUtils.isEmpty(qssj) ? TimeUtils.getFormatTimeByType(System.currentTimeMillis(), FORMAT_STR_YMD) : TimeUtils.getFormatTimeByType(TimeUtils.getTimestamp(qssj, FORMAT_STR_YMD), FORMAT_STR_YMD));
                    timeSelectorQssj.show();
                }
            });
            tvCancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    dialog.dismiss();
                }
            });
            tvSure.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String kssj = tvKsjs.getText().toString().trim();
                    String jzsj = tvJzsj.getText().toString().trim();
                    if (TextUtils.isEmpty(kssj)) {
                        ToastUtil.showMessage("起始时间不能为空");
                        return;
                    }
                    if (TextUtils.isEmpty(jzsj)) {
                        ToastUtil.showMessage("截止时间不能为空");
                        return;
                    }
                    long startTime = TimeUtils.getTimestamp(kssj, FORMAT_STR_YMD);
                    long endTime = TimeUtils.getTimestamp(jzsj, FORMAT_STR_YMD);
                    if (startTime > endTime) {
                        ToastUtil.showMessage("截止时间必须大于或等于起始时间");
                        return;
                    }
                    chooseDyTimeListener.getDyTime(startTime, endTime);
                    dialog.dismiss();

                }
            });
            dialog.setContentView(view);
            dialog.setCanceledOnTouchOutside(false);
            dialog.getWindow().setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            dialog.show();
        } catch (Exception e) {
            Log.e(TAG, "showChooseDyTimeDialog", e);
        }

    }


    public interface SettingIpListener {
        void getIp(String zwIp, String gwIp, String clusterId);
    }

    public interface ChooseDyTimeListener {
        void getDyTime(long qssj, long jzsj);
    }
}
